package com.android.baseconfig.common.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Switch;

/**
 * @author leiqingqiang
 * @description CustomSwitch 响应点击事件
 * @date 2018/2/1
 */

public class CustomSwitch extends Switch {
    private OnInterceptClick onInterceptClick;

    public CustomSwitch(Context context) {
        super(context);
    }

    public CustomSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean performClick() {
        if (onInterceptClick != null) {
            boolean b = onInterceptClick.onIntercept(this);
            if (b) {
                return true;
            }
        }
        return super.performClick();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (onInterceptClick != null) {
            return false;
        }
        return super.onTouchEvent(ev);
    }

    public void setOnInterceptClick(OnInterceptClick onInterceptClick) {
        this.onInterceptClick = onInterceptClick;
    }

    public interface OnInterceptClick {
        /**
         * 拦截点击事件
         *
         * @param v view
         * @return boolean
         */
        boolean onIntercept(View v);
    }
}
